/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../../server/Channels.h"
#include "../../server/Channel.h"
#include "Worlds.h"
#include "World.h"
#include "../../server/Players.h"
#include "../../server/Player.h"
#include "../../tools/MySQLM.h"

Worlds* Worlds::instance = 0;
short Worlds::port = port;
const char* Worlds::names [] = {"Scania", "Bera", "Broa", "Windia", "Khaini", "Bellocan", "Mardia", "Kradia", "Yellonde", "Demethos", "Elnido", "Kastia", "Judis", "Arkenia", "Plana", "Galicia", "Kalluna", "Stius", "Croa", "Zenith", "Tespia"};

void Worlds::newWorld(int id, char channels) {
	worlds.push_back(new World(id, channels));
}

Channel* Worlds::getChannelByPort(short port) {
	for (char i = 0; i < (char)worlds.size(); i++) {
		Channels* channels = worlds[i]->getChannels();
		for (char i = 0; i < channels->getChannelsCount(); i++)
			if (channels->getChannelPort(i) == port)
				return channels->getChannel(i);
	}
	return 0;
}

bool Worlds::isPlayerConnected(int charid) {
	for (char i = 0; i < (char)worlds.size(); i++)
		if (worlds[i]->isPlayerConnected(charid))
			return true;
	return false;
}

void Worlds::commandListener() {
	string cmd;
	while (getline(cin, cmd)) {
		string rep = handleCommand(cmd);
		cout << rep << endl;
	}
}

string Worlds::handleCommand(string cmd) {
	string command = cmd.substr(0, cmd.find(" "));
	if(command == "commands" || command == "help") {
		return "Commands: \n" + \
			string("shutdown | exit | quit\n") + \
			"save\n" + \
			"online";
	}
	else if(command == "shutdown" || command == "exit" || command == "quit") {
		for (char i = 0; i < (char)worlds.size(); i++) {
			Channels *channels = worlds[i]->getChannels();
			hash_map <int,Player*> *chanPlayers;
			hash_map <int,Player*>::iterator it;
			cout << "Shutting down..." << endl;
			cout << "Saving All Players..." << endl;
			for(char i = 0; i < channels->getChannelsCount(); i++) {
				chanPlayers = channels->getChannel(i)->getPlayers()->getPlayers();
				cout << "Saving channel " << (int)i << "..." << endl;
				for(it = chanPlayers->begin(); it != chanPlayers->end(); it++) {
					it->second->save();
					cout << "Saved: " << it->second->getName() << endl;
				}
			}
		}
		cout << "Saved all Players!" << endl;
		cout << "Shutting down server..." << endl;
		exit(0);
	}
	else if(command == "save") {
		for (char i = 0; i < (char)worlds.size(); i++) {
			Channels *channels = worlds[i]->getChannels();
			hash_map <int,Player*> *chanPlayers;
			hash_map <int,Player*>::iterator it;
			cout << "Saving All Players..." << endl;
			for (char i = 0; i < channels->getChannelsCount(); i++) {
				chanPlayers = channels->getChannel(i)->getPlayers()->getPlayers();
				cout << "Saving channel " << (int)i << "..." << endl;
				for (it = chanPlayers->begin(); it != chanPlayers->end(); it++) {
					it->second->save();
					cout << "Saved: " << it->second->getName() << endl;
				}
			}
		}
		cout << "Saved all Players!";
	}
	else if(command == "online") {
		for (char i = 0; i < (char)worlds.size(); i++) {
			Channels *channels = worlds[i]->getChannels();
			hash_map <int,Player*> *chanPlayers;
			hash_map <int,Player*>::iterator it;
			cout << "Online:" << endl;
			for (char i = 0; i < channels->getChannelsCount(); i++) {
				chanPlayers = channels->getChannel(i)->getPlayers()->getPlayers();
				for (it = chanPlayers->begin(); it != chanPlayers->end();it++) {
					cout << it->second->getName() << endl;
				}
			}
		}
		int amount = 0;
		for(char i = 0; i < (char)worlds.size(); i++) {
			for(char j = 0; j < worlds[i]->getChannels()->getChannelsCount(); j++) {
				amount += worlds[i]->getChannels()->getChannel(j)->getPlayers()->getPlayersCount();
			}
		}
		if (amount == 1)
			cout << "1 player is online.";
		else
			cout << amount << " players are online.";
	} else {
		return "Unknown command: " + command;
	}
	return "";
}